<template>
  <div>

    <el-table-form ref="form" :model="query">
      <el-form-item label="客户电话:">
        <el-input v-model.trim="query.targetCsmPhone" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="给合并客户电话:" class="merge-item">
        <el-input v-model.trim="query.sourceCsmPhone" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="操作时间:" :table-form-merge="2" class="merge-item">
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <template v-slot:btns>
        <el-button :loading="$loadingPlugin.fetchData" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="merge">积分合并</el-button>
        <el-button type="primary" icon="el-icon-minus" @click="correct">积分冲正</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <merge ref="scoresMerge" :csm-type-map="csmTypeMap" :csm-status-map="csmStatusMap" />
    <correct ref="correct" :csm-type-map="csmTypeMap" :csm-status-map="csmStatusMap" />
  </div>
</template>

<script>
  import { queryMap, queryScoreHandleList } from '@/api'
  import { parseTime } from '@/utils'
  import moment from 'dayjs'
  import Merge from './components/Merge'
  import Correct from './components/Correct'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'

  export default {
    name: 'scoreHandle',
    components: {
      Merge,
      Correct
    },
    data() {
      return {
        date: [moment().startOf('month'), moment().endOf('day')],
        query: {
          targetCsmPhone: '',
          sourceCsmPhone: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              prop: 'targetCsmCode',
              label: '客户编码',
              width: 110
            },
            {
              prop: 'targetCsmName',
              label: '客户名称',
              width: 110
            },
            {
              prop: 'targetCsmPhone',
              label: '客户电话',
              width: 110
            },
            {
              prop: 'sourceCsmCode',
              label: '被合并客户编码',
              width: 130
            },
            {
              prop: 'sourceCsmName',
              label: '被合并客户名称',
              width: 130
            },
            {
              prop: 'sourceCsmPhone',
              label: '被合并客户电话',
              width: 130
            },
            {
              prop: 'score',
              label: '积分数量',
              align: 'right'
            },
            {
              prop: 'dType',
              label: '操作类型',
              width: 110,
              render: (h, { row }) => <span style={this.getColor(row)}>{this.doType(row)}</span>
            },
            {
              prop: 'createTime',
              label: '操作时间',
              width: 170,
              render: (h, { row }) => <span>{ parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
            },
            {
              prop: 'createUser',
              label: '操作人'
            },
            {
              prop: 'remark',
              width: 200,
              showOverflowTooltip: true,
              label: '备注'
            }
          ],
          data: []
        },
        csmTypeMap: [],
        csmStatusMap: []
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.date && this.date.length === 2) {
          query.createTime = moment(this.date[0]).format()
          query.createTimeEnd = moment(this.date[1]).format()
        }
        const diff = moment(this.date[1]).diff(moment(this.date[0]), 'days')
        if (diff > 183) {
          this.$message.warning('日期跨度不能超过六个月')
          return
        }
        const res = await queryScoreHandleList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 查询枚举
      async queryOptionsMap() {
        const [res1, res2] = await Promise.all([mergeAsyncFunc(queryMap, { enumName: 'CustomerType' }), mergeAsyncFunc(queryMap, { enumName: 'CustomerStatus' })])
        this.csmTypeMap = res1.data // 客户类型
        this.csmStatusMap = res2.data // 客户状态
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 操作类型
      doType(row) {
        var v
        switch (row.dType) {
        case 3:
          v = '冲正(-)'
          break
        case 2:
          v = '冲正(+)'
          break
        case 1:
          v = '合并'
          break

        default:
          break
        }
        return v
      },
      // 操纵类型的颜色样式
      getColor(row) {
        var v
        switch (this.doType(row)) {
        case '冲正(-)':
          v = 'green'
          break
        case '冲正(+)':
          v = 'green'
          break
        case '合并':
          v = 'red'
          break

        default:
          break
        }
        return `color:${v}`
      },
      // 积分合并
      async merge() {
        await this.$refs.scoresMerge.open()
      },
      // 积分冲正
      async correct() {
        await this.$refs.correct.open()
      }
    }

  }
</script>

<style scoped>

</style>
